package com.lgy.httpclient;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpGetUtils {
	private static final Logger logger = LoggerFactory.getLogger(HttpGetUtils.class);

	public static String getResponseContext(String url) {
		String content = null;
		CloseableHttpClient httpclient = HttpClients.createDefault();
		// 创建httppost
		HttpGet httpget = new HttpGet(url);
		try {
			CloseableHttpResponse httpResponse = httpclient.execute(httpget);
			// 获取响应消息实体
			HttpEntity entity = httpResponse.getEntity();
			if(entity != null) {
				content = EntityUtils.toString(entity);
			}
			logger.debug("response content is : " + content);
			httpResponse.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				httpclient.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return content;
	}
}
